#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 测试文件是否存在
问题: 你想测试一个文件或目录是否存在。
提示 :
"""

import os

filename = '../hosts.txt'
print(f"{filename = }")

def recipe1():
    print(f"{os.path.exists(filename) = }")
    print(f"{os.path.exists('/tmp/spam') = }")

    # Is a regular file
    print(f"{os.path.isfile(filename) = }")

    # Is a directory
    print(f"{os.path.isdir(filename) = }")

    # Is a symbolic link 注意在windows下使用mklink命令创建连接
    print(f"{os.path.islink('../symbolic-link') = }")        
    
    # Get the file linked to
    print(f"{os.path.realpath('../symbolic-link') = }")

def recipe2():
    print(f"{os.path.getsize(filename) = }")
    print(f"{os.path.getmtime(filename) = }")

    import time
    print(f"{time.ctime(os.path.getmtime(filename)) = }")

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()

if __name__ == '__main__':
    main()           